<script setup>
import { ref } from 'vue';

const list = ref([
  {
    id: 1,
    subject: 'Vue.Js前端开发实践',
    Grade: 3,
    major: '专业课',
    status: false,
  },
]);

let remove = (id, status) => {
  if (status) {
    list.value = list.value.filter((item) => item.id !== id);
  } else {
    alert('请先完成修读再删除');
  }
};

let subject = ref('');
let Grade = ref('');
let selectedOption = ref('专业课'); 

let options = ref([
  { majorCode: '0', major: '专业课' },
  { majorCode: '1', major: '通识课' },
  { majorCode: '2', major: '选修课' },
]);

let add = () => {
  if (subject.value === '') {
    alert('课程名不能为空');
    return;
  }
  if (Grade.value === ''||Grade.value<0||Grade.value>100||isNaN(Grade.value)) {
    alert('该课程分数无效');
    return;
  }
// 计算下一个ID
  let nextId = list.value.length > 0 ? Math.max(...list.value.map(item => item.id)) + 1 : 1;

  const obj = {
    id: nextId,
    subject: subject.value,
    Grade:  Grade.value,
    major: selectedOption.value, // 使用字符串
    status: false,
  };
// 将新对象添加到列表中
  list.value.push(obj);

  subject.value = '';
  Grade.value = '';
  selectedOption.value = '专业课'; // 恢复默认值
};
</script>

<template>
  <div class="card">
    <!-- 标题区域 -->
    <div class="card-header">课程修读表</div>
    <!-- 提交区域 -->
    <div class="card-body">
      <form @submit.prevent="add">
        <div class="row g-4">
          <div class="col-auto">
            <div class="input-group mb-3">
              <span class="input-group-text" id="basic-addon1">课程名</span>
              <input type="text" class="form-control" placeholder="请输入课程名" v-model.trim="subject" />
            </div>
          </div>
          <div class="col-auto">
            <div class="input-group mb-3">
              <span class="input-group-text" id="basic-addon1">课程学分</span>
              <input type="text" class="form-control" placeholder="请输入课程分数" v-model.number="Grade" />
            </div>
          </div>
          <div class="col-auto">
            <div class="input-group mb-3">
              <span class="input-group-text" id="basic-addon1">课程种类</span>
              <select class="form-select form-select-sm" v-model="selectedOption">
                <option v-for="option in options" :key="option.majorCode" :value="option.major">{{ option.major }}</option>
              </select>
            </div>
          </div>
          <!-- 添加按钮 -->
          <div class="col-auto">
            <button type="submit" class="btn btn-primary">添加</button>
          </div>
        </div>
      </form>
    </div>
  </div>

  <!-- 表格区域 -->
  <table class="table table-striped table-hover table-bordered">
    <thead>
      <tr>
        <th scope="col">序号</th>
        <th scope="col">课程名</th>
        <th scope="col">课程分数</th>
        <th scope="col">课程种类</th>
        <th scope="col">完成状态</th>
        <th scope="col">操作</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="item in list" :key="item.id">
        <td>{{ item.id }}</td>
        <td>{{ item.subject }}</td>
        <td>{{ item.Grade }}</td>
        <td>{{ item.major }}</td>
        <td>
          <div class="form-check form-switch">
            <input
              type="checkbox"
              class="form-check-input"
              role="switch"
              :id="'cb' + item.id" 
              v-model="item.status"
            />
            <label class="form-check-label" :for="'cb' + item.id" v-if="item.status">已修读</label> 
            <label class="form-check-label" :for="'cb' + item.id" v-else>未修读</label>
          </div>
        </td>
        <td>
          <button type="submit" class="btn btn-primary" @click="remove(item.id, item.status)">删除</button>
          <!-- <a href="javascript:;" @click="remove(item.id, item.status)">删除</a> -->
        </td>
      </tr>
    </tbody>
  </table>
</template>